package juc.thread.threadPool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.LockSupport;

/*
* 不同任务类型应尽可能的创建不同的线程池运行 不然可能会导致饥饿的发生 线程池所以线程陷入阻塞
* */
public class Test6 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        List<String>dishs= Arrays.asList("宫爆鸡丁","菲力牛排","辣椒炒肉","萝卜汤","鱼子酱");
        ExecutorService restaurantCookie = Executors.newFixedThreadPool(2);
        ExecutorService restaurantWaiter = Executors.newFixedThreadPool(2);
        Random random = new Random();
        Thread main = Thread.currentThread();
        Future<String> f = restaurantCookie.submit(() -> {
            System.out.println("点餐...");
            String order = dishs.get(random.nextInt(4));
            Future<String> future = restaurantWaiter.submit(() -> {
                System.out.println("做" + order);
                return order;
            });
            System.out.println(future.get() + "上菜");
            return "";
        });
        Future<String> f1 = restaurantCookie.submit(() -> {
            System.out.println("点餐...");
            String order = dishs.get(random.nextInt(4));
            Future<String> future = restaurantWaiter.submit(() -> {
                System.out.println("做" + order);
                return order;
            });
            System.out.println(future.get() + "上菜");
            return "";
        });
        f.get();
        f1.get();
        /*try {
            restaurant.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
//        LockSupport.park();
       /* while (!restaurant.isTerminated()){
            Thread.sleep(1000);
        }*/
//        LockSupport.park();
        System.out.println("打样....");
        restaurantCookie.shutdown();
        restaurantWaiter.shutdown();
    }
}
